#include "base.h"

//
SwrConvert::SwrConvert()
{

}

void SwrConvert::SetParam(
	AVSampleFormat inputFormat, 
	AVSampleFormat outFormat,
	int inputChannel,
	int outChannel,
	int inputSamples,
	int outSamples
	)
{
	m_inputSamples = inputSamples;
	m_outSamples = outSamples;
	int64_t out_layout = av_get_default_channel_layout(outChannel);
	int64_t in_layout = av_get_default_channel_layout(inputChannel);
	m_pSwrCtx = swr_alloc_set_opts(NULL,
		out_layout, outFormat, outSamples,
		in_layout, inputFormat, inputSamples,
		0, NULL);
	swr_init(m_pSwrCtx);
}

SwrConvert::~SwrConvert()
{
	if (m_pSwrCtx)
		swr_free(&m_pSwrCtx);
}

DataPacket* SwrConvert::Convert(DataPacket* pSrcData, int size)
{
	uint8_t* pDes = new uint8_t[size];
	memset(pDes, 0, size);
	uint8_t* pOutData[4] = { pDes };
	uint8_t* pInData[4] = { pSrcData->pData };
	swr_convert(m_pSwrCtx, pOutData, m_outSamples / 100, (const uint8_t**)pInData, m_inputSamples / 100);
	SAFE_FREE_POINT(pSrcData);
	return new DataPacket(pDes, size);
}
//
_bstr_t bstr;
mutex wave_mutex;
uint8_t* pWave = new uint8_t[4096];
char* ws2str(wstring& wstr)
{
	bstr = wstr.c_str();
	return (char*)bstr;
}

wchar_t* s2wstr(string& str)
{
	bstr = str.c_str();
	return (wchar_t*)bstr;
}